package at.fhj.swd.pse.helper;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.logging.Logger;

import at.fhj.swd.pse.entities.Activity;
import at.fhj.swd.pse.entities.UserActivity;

@Stateless
public class ActivityManager {

	@PersistenceContext
	private EntityManager em;

	@Inject
	private UserManager um;

	private static final Logger logger = Logger.getLogger(ActivityManager.class);

	public ActivityManager() {

	}

	public void newUserActivity(final String message) {
		UserActivity ua = new UserActivity();
		ua.setMessage(message);
		ua.setCreator(this.um.getActiveUser());
		ua.setTimestamp(new Date(System.currentTimeMillis()));
		this.em.persist(ua);
	}

	public void removeActivity(final Activity activity) {
		this.em.remove(this.em.contains(activity) ? activity : this.em.merge(activity));
		this.em.flush();
	}

	@SuppressWarnings("unchecked")
	public List<Activity> getActivities() {
		Query q = this.em.createQuery("SELECT a FROM Activity AS a");
		return q.getResultList();
	}

}
